/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.deib.se2.meteocal.business.boundary;

import it.polimi.deib.se2.meteocal.business.boundary.LocationManager;
import it.polimi.deib.se2.meteocal.business.entity.Location;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class LocationManagerIT {
    @EJB
    LocationManager lm;
    
    @PersistenceContext
    EntityManager entity;
    
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(LocationManager.class)
                .addPackage(Location.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
    
    @Test
    public void EventManagerShouldBeInjected() {
        assertNotNull(lm);
    }
    
    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(entity);
    }
    
    @Test
    public void newLocationShouldHaveName() {
        Location l = new Location();
        l.setLocName(null);
        try {
            lm.save(l);
        } catch (Exception e) {
            //hvl what ecception is threw and then e.getCause() instanceof SpecificException
            assertTrue(true);
        }
    }
    @Test
    public void newLocationIsInWithSameIdAsInserted() {
        final String locName = "milano";
        Location l = new Location(); 
        l.setId(Long.MIN_VALUE);
        l.setLocName(locName);
        lm.save(l);
        
        assertEquals(lm.find(Long.MIN_VALUE).getLocName(), "milano");
    }
    
    public void locationsInsertedAreGet() {
        Location l1 = new Location();
        Location l2 = new Location();
        Location l3 = new Location();
        l1.setId(Long.parseLong("123"));
        l2.setId(Long.parseLong("456"));
        l3.setId(Long.parseLong("789"));
        l1.setLocName("milano");
        l2.setLocName("torino");
        l3.setLocName("roma");
        
        lm.save(l1);
        lm.save(l2);
        lm.save(l3);
        
        List<Location> list = lm.getLocations();
        
        assertTrue(list.size() == 3);
        
        boolean containsAll = true;
        
        for(Location l : list) {
            if(!l.equals(l1) || !l.equals(l2) || !l.equals(l3)) {
                containsAll = false;
            }
        }
        
        assertTrue(containsAll);
    }
    
    
}
